﻿using System;
using System.Collections.Generic;
using System.Text;

namespace OpenTools.Math
{
    /// <summary>
    /// 数学基础类
    /// 我准备先做一个各种进制随意转换器
    /// </summary>
    public class MathBase
    {
        /// <summary>
        /// 测试此类库可以被调用
        /// </summary>
        /// <returns></returns>
        public static string TestCanUse()
        {
            return $"Hello ,this is my Tools.";
        }

        /// <summary>
        /// 把任意进制的数字转换为10进制的
        /// </summary>
        /// <param name="value"></param>
        /// <param name="typeNow">当前数据的进制值：当前仅支持2-10</param>
        /// <returns></returns>
        public static int To10X(string value, int typeNow)
        {
            if (typeNow > 16 || typeNow < 2)
            {
                throw new Exception("目标进制类型请在2-16内任选择");
            }
            int result = 0;

            
            string data = value.ToString(); 
            int index = 0;
            for (int i = data.Length - 1; i > -1; i--)
            {
                string s = data[i].ToString();
                s = ZiMuBiaoShi(s);

                result = result + Convert.ToInt32(s) * Convert.ToInt32(System.Math.Pow(typeNow, index));
                index++;
            }


            return result;
        }

        /// <summary>
        /// 10进制数据转换成其他进制数据
        /// </summary>
        /// <param name="value"></param>
        /// <param name="typeTo">当前数据的进制值：当前仅支持2-10</param>
        /// <returns></returns>
        public static string C10X_ToOthers(int value, int typeTo)
        {
            if (typeTo > 16 || typeTo < 2)
            {
                throw new Exception("目标进制类型请在2-16内任选择");
            }

            string result = "";

            List<int> shuzhiList = new List<int>();

            int index = 0;
            while (true)
            {
                int di = Convert.ToInt32(System.Math.Pow(typeTo, index));
                if (value >= di)
                {
                    var num = (value / di) % typeTo;
                    shuzhiList.Add(num);
                }
                else
                {
                    break;
                }
                index++;


            }

            string data = "";
            for (int i = 0; i < shuzhiList.Count; i++)
            {
                if (shuzhiList[i]<10)
                {
                    data = $"{shuzhiList[i]}{data}";
                }
                else
                {
                    string letter = ShuZiZhuanHuan(shuzhiList[i]);
                    data = $"{letter}{data}";
                }
                
            }

            if (data == "")
            {
                data = "0";
            }
            result = data;

            return result;
        }


        private static string ZiMuBiaoShi(string letter)
        {
            string result = "-";
            switch (letter)
            {
                case "0": result = "0"; break;
                case "1": result = "1"; break;
                case "2": result = "2"; break;
                case "3": result = "3"; break;
                case "4": result = "4"; break;
                case "5": result = "5"; break;
                case "6": result = "6"; break;
                case "7": result = "7"; break;
                case "8": result = "8"; break;
                case "9": result = "9"; break;
                case "A": result = "10"; break;
                case "B": result = "11"; break;
                case "C": result = "12"; break;
                case "D": result = "13"; break;
                case "E": result = "14"; break;
                case "F": result = "15"; break;
                default:
                    break;
            }
            return result;
        }

        private static string ShuZiZhuanHuan(int num)
        {
            string result = "-";
            switch (num)
            {
                case 10: result = "A"; break;
                case 11: result = "B"; break;
                case 12: result = "C"; break;
                case 13: result = "D"; break;
                case 14: result = "E"; break;
                case 15: result = "F"; break;
                default:
                    break;
            }
            return result;
        }
    }
}
